local skel = fk.CreateSkill {
  name = "lb_zero__mengyao_high_zhuangjiahati",
}

Fk:loadTranslationTable {
  ["lb_zero__mengyao_high_zhuangjiahati"] = "恶名·装甲哈提",
  [":lb_zero__mengyao_high_zhuangjiahati"] = "首次获得此技能时，你可以从场上或弃牌堆中依次选择两张装备牌获得并可以使用之。当你受到伤害后，你可以重铸一张装备牌，摸三张牌并可以使用一张牌。",

  ["#lb_zero__mengyao_high_zhuangjiahati"] = "哈提：你可以重铸一张装备牌，摸三张牌并可以使用一张牌",
  ["#lb_zero__mengyao_high_zhuangjiahati-ask"] = "哈提（%arg/2）：你可以从场上或弃牌堆中选择一张装备牌获得并可以使用之",
  ["#lb_zero__mengyao_high_zhuangjiahati-obtian"] = "哈提（%arg/2）：选择 %src 一张装备牌获得并可以使用之",
  ["#lb_zero__mengyao_high_zhuangjiahati-use"] = "哈提：你可以使用 %arg",

  ["$lb_zero__mengyao_high_zhuangjiahati1"] = "",
  ["$lb_zero__mengyao_high_zhuangjiahati2"] = "",
}


skel:addEffect(fk.Damaged, {
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(skel.name) and not player:isNude()
  end,
  on_cost = function(self, event, target, player, data)
    local cards = player.room:askToCards(player, {
      min_num = 1,
      max_num = 1,
      skill_name = skel.name,
      include_equip = true,
      pattern = ".|.|.|.|.|equip",
      prompt = "#lb_zero__mengyao_high_zhuangjiahati",
    })
    if #cards > 0 then
      event:setCostData(self, { cards = cards })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    data.extra_data = data.extra_data or {}
    data.extra_data.lb_zero__huiyin = true
    local room = player.room
    room:recastCard(event:getCostData(self).cards, player, skel.name)
    if player:isAlive() then
      player:drawCards(3, skel.name)
      if player:isAlive() then
        room:askToPlayCard(player, {
          skill_name = skel.name,
        })
      end
    end
  end,
})

skel:addAcquireEffect(function(self, player, is_start)
  if player.room:addTableMarkIfNeed(player, "lb_zero__mengyao_high_skills", skel.name) then
    local room = player.room
    room:broadcastPlaySound("./packages/lb_kill_zero/audio/skill/mengyao_audio/yihai_" .. math.random(5, 6))
    for i = 1, 2, 1 do
      local ids = table.filter(room.discard_pile, function(id)
        return Fk:getCardById(id).type == Card.TypeEquip
      end)
      local tos = table.filter(room.alive_players, function(to)
        return table.find(to:getCardIds("e"), function(id)
          return Fk:getCardById(id).type == Card.TypeEquip
        end) ~= nil
      end)
      if #ids > 0 or #tos > 0 then
        local s, ret = room:askToUseActiveSkill(player, {
          skill_name = "#lb_zero__mengyao_high_zhuangjiahati-choose",
          cancelable = true,
          prompt = "#lb_zero__mengyao_high_zhuangjiahati-ask:::" .. i,
          extra_data = { extra_data = { ids = ids } }
        })
        if s and ret then
          local cards = {}
          if ret.cards and #ret.cards > 0 then
            cards = ret.cards
            room:obtainCard(player, ret.cards, true, fk.ReasonJustMove, player, skel.name)
          else
            local to = ret.targets[1]
            local card = room:askToChooseCard(player, {
              target = to,
              flag = { card_data =  { { to:__touistring(), table.filter(to:getCardIds("e"), function(id)
                return Fk:getCardById(id).type == Card.TypeEquip
              end) } } },
              skill_name = skel.name,
              pattern = "",
              prompt = "#lb_zero__mengyao_high_zhuangjiahati-obtian:" .. to.id .. "::" .. i,
            })
            cards = { card }
            room:obtainCard(player, card, true, fk.ReasonPrey, player, skel.name)
          end
          if player.dead then return end
          room:askToUseRealCard(player, {
            pattern = cards,
            skill_name = skel.name,
            cancelable = true,
            prompt = #cards == 1 and ("#lb_zero__mengyao_high_zhuangjiahati-use:::" .. Fk:getCardById(cards[1]):toLogString()) or nil
          })
          if player.dead then return end
        else
          break
        end
      end
    end
  end
end)

return skel
